﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;
using System.Windows.Forms;

namespace Cotizav
{
    public class CtrlMotivo
    {
        ConexionDataContext conexion;

        internal List<MOTIVO> getListMotivo()
        {
            using (conexion = new ConexionDataContext())
            {
                try
                {
                    return conexion.MOTIVO.Where(x => x.eliminado == 0).ToList();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        internal System.Windows.Forms.DialogResult registrarMotivo(MOTIVO motivo)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        DialogResult result = DialogResult.Cancel;

                        int correlativoMotivo = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").MOTIVO.Value;
                        string num = Convert.ToString(correlativoMotivo);
                        string numero = num.Trim().PadLeft(11, '0');
                        string idMotivo = "MOT_" + numero;

                        motivo.idMotivo = idMotivo;

                        conexion.MOTIVO.InsertOnSubmit(motivo);
                        conexion.SubmitChanges();
                        conexion.increm_ContMotivo();

                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal DialogResult actualizarMotivo(MOTIVO motivo)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.MOTIVO mot = conexion.MOTIVO.Single(x => x.idMotivo == motivo.idMotivo);
                        DialogResult result = DialogResult.Cancel;
                        //actualizamos
                        mot.tipoMotivo = motivo.tipoMotivo;                        

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarCategoria(MOTIVO mot)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.MOTIVO moti = conexion.MOTIVO.Single(x => x.idMotivo == mot.idMotivo);
                        moti.eliminado = mot.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
    }
}
